package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.horo.zhuizhui.adapter.MustBeChasedAdapter;
import com.lanou.horo.zhuizhui.entity.MustBeChasedItem;
import com.lanou.horo.zhuizhui.utility.recyclerView.SpacesItemDecoration;
import com.lanou.horo.zhuizhui.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 */
public class MustBeChasedActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private RequestQueue queue;
    private ImageView backBtn;
    private MustBeChasedAdapter adapter;
    private TextView titleTv;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chased_be_must);
        initView();
        initData();
    }

    private void initView() {
        titleTv = (TextView) findViewById(R.id.mustBeChasedTiTle);
        backBtn = (ImageView) findViewById(R.id.mbcBackBtn);
        backBtn.setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.mustBeChasedRecyclerView);
        adapter = new MustBeChasedAdapter(this);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new SpacesItemDecoration(-6));
        recyclerView.setHasFixedSize(true);
    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        Intent intent = this.getIntent();
        int id = intent.getIntExtra("id", 0);
        switch (id) {
            case 44:   //每日漫画推荐
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                        "1445907216338&param=%7B%22id%22%3A%22540%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                        "count%22%3A18%2C%22page%22%3A1%7D&sig=a4339e207e507957cb3ce21dbb5b1954";
                break;
            case 50:  //国漫
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                        "1445244046395&param=%7B%22id%22%3A%22387%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                        "count%22%3A18%2C%22page%22%3A1%7D&sig=0ede8d1f464ddbadb7f3811fc7ed6e7c";
                break;
            case 35:  //少女爱情
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                        "1446003553881&param=%7B%22id%22%3A%22376%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                        "count%22%3A18%2C%22page%22%3A1%7D&sig=b62e7ae508a658e646980592d7b1240b";
                break;
            case 33:  //少年热血
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                        "1446003641015&param=%7B%22id%22%3A%22393%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                        "count%22%3A18%2C%22page%22%3A1%7D&sig=cfd0c14439e5efff2bdba4f369507491";
                break;
            case 37:  //国漫
                url = "http://api.zhuizhuiyoyo.com/request.php?method=theme_info&timestamp=" +
                        "1445244046395&param=%7B%22id%22%3A%22387%22%2C%22v%22%3A1%2C%22t%22%3A0%2C%22" +
                        "count%22%3A18%2C%22page%22%3A1%7D&sig=0ede8d1f464ddbadb7f3811fc7ed6e7c";
                break;
        }

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();

                MustBeChasedItem mbci = gson.fromJson(response, MustBeChasedItem.class);

                if (mbci != null) {
                    List<MustBeChasedItem.ComicsEntity> list = mbci.getComics();
                    titleTv.setText(mbci.getTheme().getName());
                    if (list != null && list.size() > 0) {

                        adapter.addComList(list);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mbcBackBtn:
                finish();
                break;
        }
    }
}
